package com.grasp.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.grasp.activity.R;
import com.grasp.data.IconItem;

public class IconAdapter extends BaseAdapter {
	private List<IconItem> mDatas;

	public IconAdapter(Context context, List<IconItem> datas) {
		mDatas = datas;
	}

	@Override
	public int getCount() {
		return mDatas.size();
	}

	@Override
	public Object getItem(int position) {
		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ItemHolder mHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(parent.getContext()).inflate(
					R.layout.bottom_dialog_item, null);
			mHolder = new ItemHolder();
			mHolder.icon = (ImageView) convertView
					.findViewById(R.id.dialog_item_icon);
			mHolder.title = (TextView) convertView
					.findViewById(R.id.dialog_item_title);
			mHolder.title.setVisibility(View.GONE);
			int width = parent.getContext().getResources().getDisplayMetrics().widthPixels;
			AbsListView.LayoutParams params = new AbsListView.LayoutParams(
					width / 4, width / 4);
			mHolder.icon.getLayoutParams().height = width / 6;
			mHolder.icon.getLayoutParams().width = width / 6;
			convertView.setLayoutParams(params);
			convertView.setTag(mHolder);
		} else {
			mHolder = (ItemHolder) convertView.getTag();
		}
		IconItem mItem = mDatas.get(position);
		int id = convertView.getResources().getIdentifier(mItem.getFileName(),
				"drawable", convertView.getContext().getPackageName());
		mHolder.icon.setImageResource(id);
		return convertView;
	}

	class ItemHolder {
		private ImageView icon;
		private TextView title;
	}

}
